import $http from '@/ajax/http.js';

export const loadingImage = {
	data() {
		return {
			urlImage: "",
			failId: '',
			workerId:'',
			token:wx.getStorageSync("token")
		}
	},
	onLoad(options){
		this.workerId = options.workerId
		console.log(options);
	},
	methods: {
		// 选择图片并上传
		chooseAndUploadImage(sourceType) {
			let that = this;
			wx.chooseImage({
				count: 1,
				sizeType: ['original'],
				sourceType: [sourceType],
				success(res) {
					that.urlImage = res.tempFilePaths[0];
				},
			});
		},
		// 拍摄照片
		shoot() {
			this.chooseAndUploadImage('camera');
		},
		// 从相册选择照片
		photo() {
			this.chooseAndUploadImage('album');
		},

		signImage() {
			const that = this
			wx.showLoading({
				title: '正在上传图片',
				mask: true
			});
			wx.uploadFile({
				url: 'https://66945d7d.r23.cpolar.top/waiter/index/uploadScenePic',
				header: {
					'content-type': 'multipart/form-data',
					Authorization: that.token,
				},
				filePath: that.urlImage,
				name: 'file',
				formData:{
					workOrderId:that.workerId
				},
				success: (uploadRes) => {
					if (uploadRes.statusCode === 200) {
						wx.showToast({
							title: '图片上传成功',
						});
						const data = JSON.parse(uploadRes.data);
						// 根据服务器返回的数据处理逻辑  
						console.log(data, "上传成功");
						that.urlImage = "";
						that.failId = data.data.uploadFileId
					} else {
						console.log(uploadRes, "上传失败，状态码不是200");
						wx.showToast({
							title: '图片上传失败！请重新上传',
						});
					}
				}
			});
		},
	}

}